<?php

session_start();

if(isset($_SESSION['user'])){
  header('location: index.php');
  exit;
}

$error = Null;
if($_GET && isset($_GET['exists'])){
  $error = "Username already exists!  Please try again.";
}
  
?>

<?php $onload_function = "init();" ?>
<?php require_once("header.php"); ?>

<script type="text/javascript">

function init(){
  password_check();
}

function password_check(){
  var pass1 = document.getElementById("new_pass1").value;
  var pass2 = document.getElementById("new_pass2").value;
  var submit = document.getElementById("pass_submit");
  var pass_flag = document.getElementById("pass_flag");
  var reason = document.getElementById("pass_reason");
  
  if(pass1 == '' && pass2 == ''){
    pass_flag.innerHTML = '';
    reason.innerHTML = '<br />';
    submit.disabled = true;
  }
  else if(pass1 == ''){
    pass_flag.innerHTML = '<img src="layout/password_no.jpg" />';
    reason.innerHTML = 'Please enter a password in both fields.';
    submit.disabled;
  }
  else if(pass1 != pass2){
    pass_flag.innerHTML = '<img src="layout/password_no.jpg" />';
    reason.innerHTML = 'Passwords do not match.';
    submit.disabled = true;
  }
  else if(pass1 == pass2){
    if(pass1.length < 5){
      pass_flag.innerHTML = '<img src="layout/password_no.jpg" />';
      reason.innerHTML = 'Password too short.';
      submit.disabled = true;
    }
    else{
      pass_flag.innerHTML = '<img src="layout/password_yes.jpg" />';
      reason.innerHTML = '<br />';
      submit.disabled = false;
    }
  }
}

</script>

<?php

if(isset($error)){
  echo '<div class="error">' . $error . '</div>';
}

?>
<img id = "title" src = "./layout/create.png">
<p class = "text">Join today! It is simple and easy to create an account.  Within minutes you can explore data on apartments across the nation. Enter a username and password
below to begin your search.  Please read over our <a href = "termsofservice.php">Terms of Service</a>.</p>

<div id = "bigbox">
	<div id = "createcurved"></div>
	<div id = "createbox">
		
		<table>
				<form method="post" action="docreate.php">
			<tr>
					<td>Username: </td><td><input class="width" name="username" type="text" /></td>
			</tr>
			<tr>
				  <td>Password: </td><td><input class="width" type="password" name="password" id="new_pass1" onkeydown="password_check();" onkeyup="password_check();" onblur="password_check();" /></td>
			</tr>
			<tr>
				  <td>Confirm:</td><td><input class="width" type="password" id="new_pass2" onkeydown="password_check();" onkeyup="password_check();" onblur="password_check();" /></td><td><div id="pass_flag"></div></td>
			</tr>
			<tr>	
				  <td></td><td><input type="submit" id="pass_submit" value="Create!" /></td>
				  
			</tr>
			<tr>
				<td colspan = "3"><span id="pass_reason" class="red"></span></td>
			</tr>
				</form>
		</table>
	</div>
</div>


<?php require_once("footer.php");
